/*************************************************************************
*                              起源太空
*
*------------------------------------------------------------------------
* 文件名：		ps_can.c
* 使用说明：
*           用于ps端的CAN通信
*------------------------------------------------------------------------
* 日期： 		2022.11.01
* 版本： 		V0.0
* 作者：		张海峰
*------------------------------------------------------------------------
* 					修改 / 更新记录
*------------------------------------------------------------------------
* 日期： 		2022.10.18
* 版本： 		V0.1
* 修改人：		XXX
* 修改内容：	1.添加了XXX XXX 功能
*           2.修改了XXX XXX 的错误
*------------------------------------------------------------------------
* 日期： 		2022.10.19
* 版本： 		V0.2
* 修改人：		XXX
* 修改内容：	1.添加了XXX XXX 功能
*           2.修改了XXX XXX 的错误
*************************************************************************/



/*************************************************************************
*                               头文件
*************************************************************************/
#include <admin_firmware/admin_firmware.h>
#include "gnc_addit.h"
#include "xparameters.h"

#include <string.h>
#include "dri_hal_can.h"

#include "dri_hal_uart_lite.h"
#include "camera_process.h"
#include "fpga_reg.h"
#include "update_firmware.h"
#include "crc16.h"
#include "tele_data_transfer.h"
#include "cali_panel.h"
#include "lens_cap.h"
#include "pl_comm.h"
#include "hot_knife.h"
#include "sys_monitor.h"
#include "admin_firmware.h"
/*************************************************************************
*                           全局变量 \ 引用
*************************************************************************/
S_tCanData tCanRecvData;					/* can接口接收的数据 */
S_tCanData tCanRecvGNCData;                 /* can接口接收的GNC数据 */

S_tCanData tCanSendData;					/* can接口发送的数据 */

extern S_tSysMonit s_SysMonit;
extern S_tSysREV s_SysRev;
extern S_tFirmwareUpdate s_FirmwareUpdate;
extern S_tAdditData s_Addit1Data;
extern S_tAdditData s_Addit2Data;
extern enum E_CAMERA_SHOOT  Camera1ShootState;
extern enum E_CAMERA_SHOOT  Camera2ShootState;
extern S_PREP_PARAM_SAVE s_CamPrepFlag;
extern S_tAdFw s_AdFw;
extern S_CALIData s_Cam1CaliData;
/*************************************************************************
*                           本地变量 \ 引用
*************************************************************************/
#define CAN_DATE_COMPLETED				1
#define CAN_DATE_UNCOMPLETED			0

enum
{
	PRETREATMENT_ID = 0,
	CAMERA_ONE_ID,
	CAMERA_TWO_ID,
	MOTOR_ID,
	LED_ID,
    HOT_ID,
    CMA_1_PREP,
    CMA_2_PREP,
}E_DEVICE_ID;


static u32 TxFrame[4]={0};
static u32 RxFrame[4]={0};

#if 1
static u8 s_ucSN = 0;										/* 帧序号 */
#endif

static void PsCanSendHandler(void *CallBackRef);
static void PsCanRecvHandler(void *Callback);
static void PsCanMsgType (XCanPs *InstancePtr, u32 *puiRxFrame, u16 usID, u8 ucDLC);
static void PsCanDataProcess(XCanPs *InstancePtr, u32 *puiRxFrame, u16 usID, u8 ucDLC);
void PsCanSendSingleFrameResult(XCanPs *InstancePtr, u32 uiId, u8 Result);
static void PsCanCameraCommandProcess(u8 ucCommand, u8 *puacData, u8 ucPlUartNum);
void PsCanCommandProcess(u32 *puiRxFrame);
static void PsCanLedCommandProcess(u8 ucCommand, u8 *puacData);
static void PsCanMotorCommandProcess(u8 ucCommand, u8 *puacData);
static void PsCanPretreatmentCommandProcess(u8 ucCommand, u8 *puacData);
void GncDataProcess(XCanPs *InstancePtr, u32 *puiRxFrame, u16 usID, u8 ucDLC);
int PsCanSendDataMultiFirstFrame(XCanPs *InstancePtr, u8* pucFrameData, u32 uiLength, u8 ucFn, u8 ucKd);
int PsCanSendDataMultiMiddleFrame(XCanPs *InstancePtr, u8* pucFrameData, u32 uiLength, u8 ucSn);
int PsCanSendDataMultiLastFrame(XCanPs *InstancePtr, u8* pucFrameData, u32 uiLength, u8 ucSn);
void TimeSyncDataProcess(u32 *puiRxFrame);
int PsCanIdFiliter(XCanPs *InstancePtr, u32 uiFilterIndexs, u32 uiMaskValue, u32 uiIdValue);
/*************************************************************************
*                              函数原型
*************************************************************************/

/*************************************************************************
* 函数名称：	PsCanInit
* 功能：		ps端can接口的初始化
**************************************************************************
* 全局变量：
* 输入变量：	usDeviceId					设备号
* 			CanInstPtr					can设备指针
* 			IntcInstancePtr				设备中断处理设备指针
* 			usCanIntrId					ps can中断号
* 输出变量：		无
* 返回值：		iStatus:
* 				XST_SUCCESS		0		成功
* 				XST_FAILURE		1		失败
*-----------------------------------------------------------------------
* 	日期         		 修改人       		 修改内容
*-----------------------------------------------------------------------
* 2022.10.31   	张海峰         		 创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
int PsCanInit(u16 usDeviceId, XCanPs *CanInstPtr, XScuGic *IntcInstancePtr,
			 u16 usCanIntrId)
{

    u32 timeoutCnt = U_SECOND_COUNT*1000*1000;//1s
 	/* 状态变量  */
	int iStatus;

 	/* PS CAN 配置寄存器指针 */
 	XCanPs_Config *ConfigPtr;

 	/* 查找设备can的ID  */
    ConfigPtr = XCanPs_LookupConfig(usDeviceId);
 	if (CanInstPtr == NULL){
 		return XST_FAILURE;
 	}

 	/* 初始化can驱动  */
 	iStatus = XCanPs_CfgInitialize(CanInstPtr, ConfigPtr, ConfigPtr->BaseAddr);
 	if (iStatus != XST_SUCCESS){
 		return XST_FAILURE;
 	}

 	/* 初始化检查自测  */
 	iStatus = XCanPs_SelfTest(CanInstPtr);
 	if (iStatus != XST_SUCCESS){
 		return XST_FAILURE;
 	}

 	/* 设置CAN端口为配置模式  */
 	XCanPs_EnterMode(CanInstPtr, XCANPS_MODE_CONFIG);
 	while(XCanPs_GetMode(CanInstPtr) != XCANPS_MODE_CONFIG && timeoutCnt--);

 	/* 设置波特率、 比特时间   */
 	XCanPs_SetBaudRatePrescaler(CanInstPtr, TEST_BRPR_BAUD_PRESCALAR);

 	XCanPs_SetBitTiming(CanInstPtr, TEST_BTR_SYNCJUMPWIDTH,TEST_BTR_SECOND_TIMESEGMENT,TEST_BTR_FIRST_TIMESEGMENT);
// 	iStatus = PsCanInterruptSet(IntcInstancePtr,CanInstPtr, usCanIntrId);
// 	if (iStatus != XST_SUCCESS){
// 	   return XST_FAILURE;
//	}
 	/* 配置can端口为normal模式  */
 	XCanPs_EnterMode(CanInstPtr, XCANPS_MODE_NORMAL);
    timeoutCnt = U_SECOND_COUNT*1000*1000;
 	while(XCanPs_GetMode(CanInstPtr) != XCANPS_MODE_NORMAL && timeoutCnt--);

 	/* 初始化can端口数据  */
 	tCanRecvData.usSize = 0;
 	memset(tCanRecvData.uacData, 0, sizeof(tCanRecvData));
 	iStatus = PsCanIdFiliter(CanInstPtr, XCANPS_AFR_UAF1_MASK, (u32)(0x7c << 21u), (u32)(YW2_PREP_TELE_DATA_CAN_ADDR_ID << 23u));
 	if (iStatus != XST_SUCCESS){
 	   return XST_FAILURE;
	}
 	iStatus = PsCanIdFiliter(CanInstPtr, XCANPS_AFR_UAF2_MASK, (u32)(0x7c << 21u), (u32)(YW2_PREP_TELE_DATA_CAN_BC_ADDR_ID << 23u));
 	if (iStatus != XST_SUCCESS){
 	   return XST_FAILURE;
	}
 	memset(&tCanRecvData, 0, sizeof(tCanRecvData));

 	return iStatus;
}

/*************************************************************************
* 函数名称：	PsCanInterruptSet
* 功能：		设置ps can的中断
**************************************************************************
* 全局变量：		无
* 输入变量：
* 			IntcInstancePtr			gic中断配置指针
* 			CanInstancePtr			can中断配置指针
* 			usCanIntrId				can中断配置的中断号
* 输出变量：		无
* 返回值：
* 			iStatus:
* 				XST_SUCCESS		0		成功
* 				XST_FAILURE		1		失败
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.31   	 张海峰          		创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
int PsCanInterruptSet(XScuGic *IntcInstancePtr, XCanPs *CanInstancePtr,
 						 u16 usCanIntrId)
{
	/* 状态变量  */
	int iStatus = XST_FAILURE;

	/* 系统中断寄存器配置指针  */
 	XScuGic_Config *IntcConfig;

 	/* 配置中断收发回调函数  */
 	XCanPs_SetHandler(CanInstancePtr, XCANPS_HANDLER_SEND,
 			(void *)PsCanSendHandler, (void *)CanInstancePtr);

 	XCanPs_SetHandler(CanInstancePtr, XCANPS_HANDLER_RECV,
 			(void *)PsCanRecvHandler, (void *)CanInstancePtr);

 	Xil_ExceptionInit();

 	/* 初始化中断控制器驱动  */
 	IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
 	if (NULL == IntcConfig)
 	{
 		return XST_FAILURE;
 	}

 	iStatus = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
 					IntcConfig->CpuBaseAddress);

 	if (iStatus != XST_SUCCESS)
 	{
 		return XST_FAILURE;
 	}

 	/* 配置中断号的优先级  */
 	XScuGic_SetPriorityTriggerType(IntcInstancePtr, usCanIntrId,
 									0x01, 0x3);

 	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
 				(Xil_ExceptionHandler)XScuGic_InterruptHandler,
 				IntcInstancePtr);

 	/* 连接中断处理函数与中断号  */
 	iStatus = XScuGic_Connect(IntcInstancePtr, usCanIntrId,
 					(Xil_InterruptHandler)XCanPs_IntrHandler,
					(void *)CanInstancePtr);

 	if (iStatus != XST_SUCCESS)
 	{
 		return iStatus;
 	}

 	/* 使能can中断  */
 	XScuGic_Enable(IntcInstancePtr, usCanIntrId);
 	Xil_ExceptionEnable();
	XCanPs_IntrEnable(CanInstancePtr, XCANPS_IXR_RXNEMP_MASK|XCANPS_IXR_TXOK_MASK);

 	return XST_SUCCESS;
}


/*************************************************************************
* 函数名称：	PsCanSendSingleFrame
* 功能：		ps can发送数据单帧
**************************************************************************
* 全局变量：		无
* 输入变量：
* 			InstancePtr					ps can设备的指针
* 输出变量：		无
* 返回值：
* 			iStatus:
* 				XST_SUCCESS		0		成功
* 				XST_FAILURE		1		失败
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.20   	 董瑞          		创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
int PsCanSendSingleFrame(XCanPs *InstancePtr, u32* puiFrameData, u32 uiLength, u32 uiId)
 {
	/* 状态变量  */
 	int iStatus;

 	/* 帧ID，固存单机 */
 	TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)uiId, 0, 0, 0, 0);

 	/* 帧长度DLC֡ */
 	TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)uiLength);

 	/* 帧数据 Byte 0-7 */
 	TxFrame[2] = puiFrameData[0];
 	TxFrame[3] = puiFrameData[1];

 	iStatus = XCanPs_Send(InstancePtr, TxFrame);
 	return iStatus;
 }

/*************************************************************************
* 函数名称：	PsCanSendMultiFirstFrame
* 功能：		ps can发送数据多帧的首帧
**************************************************************************
* 全局变量：		无
* 输入变量：
* 			InstancePtr					ps can设备的指针
* 输出变量：		无
* 返回值：
* 			iStatus:
* 				XST_SUCCESS		0		成功
* 				XST_FAILURE		1		失败
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.20   	 董瑞          		创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
int PsCanSendDataMultiFirstFrame(XCanPs *InstancePtr, u8* pucFrameData, u32 uiLength, u8 ucFn, u8 ucKd)
 {
	/* 状态变量  */
 	int iStatus;

 	/* 帧ID，固存单机 */
 	TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)YW2_PREP_TELE_DATA_CAN_FIRST_ID,0,0,0,0);

 	/* 帧长度DLC֡ */
 	TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)uiLength);

 	/* 帧数据 Byte 0-7 */
 	TxFrame[2] = 1; //序号从1开始
 	TxFrame[2] |= (ucFn << 8);
 	TxFrame[2] |= (ucKd << 16);
 	TxFrame[2] |= (pucFrameData[0] << 24);

 	TxFrame[3] = pucFrameData[1];
 	TxFrame[3] |= (pucFrameData[2] << 8);
 	TxFrame[3] |= (pucFrameData[3] << 16);
 	TxFrame[3] |= (pucFrameData[4] << 24);

 	iStatus = XCanPs_Send(InstancePtr, TxFrame);
 	return iStatus;
 }

/*************************************************************************
* 函数名称：	PsCanSendMultiFirstFrame
* 功能：		ps can发送数据多帧的首帧
**************************************************************************
* 全局变量：		无
* 输入变量：
* 			InstancePtr					ps can设备的指针
* 输出变量：		无
* 返回值：
* 			iStatus:
* 				XST_SUCCESS		0		成功
* 				XST_FAILURE		1		失败
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.20   	 董瑞          		创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
int PsCanSendDataMultiMiddleFrame(XCanPs *InstancePtr, u8* pucFrameData, u32 uiLength, u8 ucSn)
 {
	/* 状态变量  */
 	int iStatus;

 	/* 帧ID，固存单机 */
 	TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)YW2_PREP_TELE_DATA_CAN_MID_ID,0,0,0,0);

 	/* 帧长度DLC֡ */
 	TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)uiLength);

 	/* 帧数据 Byte 0-7 */
 	TxFrame[2] = ucSn;
 	TxFrame[2] |= (pucFrameData[0] << 8);
 	TxFrame[2] |= (pucFrameData[1] << 16);
 	TxFrame[2] |= (pucFrameData[2] << 24);

 	TxFrame[3] = pucFrameData[3];
 	TxFrame[3] |= (pucFrameData[4] << 8);
 	TxFrame[3] |= (pucFrameData[5] << 16);
 	TxFrame[3] |= (pucFrameData[6] << 24);

 	iStatus = XCanPs_Send(InstancePtr, TxFrame);
 	return iStatus;
 }

/*************************************************************************
* 函数名称：	PsCanSendMultiFirstFrame
* 功能：		ps can发送数据多帧的首帧
**************************************************************************
* 全局变量：		无
* 输入变量：
* 			InstancePtr					ps can设备的指针
* 输出变量：		无
* 返回值：
* 			iStatus:
* 				XST_SUCCESS		0		成功
* 				XST_FAILURE		1		失败
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.20   	 董瑞          		创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
int PsCanSendDataMultiLastFrame(XCanPs *InstancePtr, u8* pucFrameData, u32 uiLength, u8 ucSn)
 {
	/* 状态变量  */
 	int iStatus;

 	/* 帧ID，固存单机 */
 	TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)YW2_PREP_TELE_DATA_CAN_LAST_ID,0,0,0,0);

 	/* 帧长度DLC֡ */
 	TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)uiLength);

 	/* 帧数据 Byte 0-7 */
 	TxFrame[2] = ucSn;
 	TxFrame[2] |= (pucFrameData[0] << 8);
 	TxFrame[2] |= (pucFrameData[1] << 16);
 	TxFrame[2] |= (pucFrameData[2] << 24);

 	TxFrame[3] = pucFrameData[3];
 	TxFrame[3] |= (pucFrameData[4] << 8);
 	TxFrame[3] |= (pucFrameData[5] << 16);
 	TxFrame[3] |= (pucFrameData[6] << 24);

 	iStatus = XCanPs_Send(InstancePtr, TxFrame);
 	return iStatus;
 }

/**      Result =        XST_SUCCESS     0       成功
*        Result =        XST_FAILURE     1       失败*/
void PsCanSendSingleFrameResult(XCanPs *InstancePtr, u32 uiId, u8 Result)
{
    u32 auiSendResult[2] = {0};
    if(Result == XST_SUCCESS)
    {
        auiSendResult[0] = 0;
    }
    else if(Result == XST_FAILURE)
    {
        auiSendResult[0] = 0xFFFF;
    }
    PsCanSendSingleFrame(InstancePtr, auiSendResult, 1, uiId);

}


/*************************************************************************
* 函数名称：	PsCanSendDataMsg
* 功能：		ps can发送遥测数据
**************************************************************************
* 全局变量：		无
* 输入变量：
* 			InstancePtr					ps can设备的指针
* 输出变量：		无
* 返回值：
* 			iStatus:
* 				XST_SUCCESS		0		成功
* 				XST_FAILURE		1		失败
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2023.01.06   	张海峰          		创建
* 2023.01.12    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void PsCanSendDataMsg(XCanPs *InstancePtr,u32 *puiRxFrame)
{
	int iIndex = 0;
	int iNumber = 0;
	int iTempIndex = 0;
	u8 acMiddleMsg[7] = {0};
	u8 teleType = 0;
	u8 totalFn;

	teleType = puiRxFrame[2] & 0xff;
	if(teleType == TELE_TYPE_ROUTINE){
		TeleRoutineStatusMsg(&tCanSendData);
		totalFn = ((tCanSendData.usSize-5)/7)+2;//计算帧总数
		g_tEquipmentStatus.canCompleOrder ++;
	}
	else if(teleType == TELE_TYPE_FW){
		TeleFwStatusMsg(&tCanSendData);
		totalFn = ((tCanSendData.usSize-5)/7)+2;//计算帧总数
		g_tEquipmentStatus.canCompleOrder ++;
	}
	else if(teleType == TELE_TYPE_PREP){
		TelePrepStatusMsg(&tCanSendData);
		totalFn = ((tCanSendData.usSize-5)/7)+2;//计算帧总数
		g_tEquipmentStatus.canCompleOrder ++;
	}
	else if(teleType == TELE_TYPE_MASK) {
        TeleMaskStatusMsg(&tCanSendData);
        totalFn = ((tCanSendData.usSize - 5) / 7) + 2; //计算帧总数
        g_tEquipmentStatus.canCompleOrder++;
    }
	else{
		return;
	}

	/* 发送首帧  */
	PsCanSendDataMultiFirstFrame(InstancePtr, tCanSendData.uacData, 8, totalFn, teleType);
    iTempIndex = 4;
	/* 发送中间帧 */
	for(iIndex = 0; iIndex < (totalFn - 2); iIndex++)
	{
		for(iNumber = 0; iNumber < 7; iNumber++)
		{
			iTempIndex = (iIndex * 7 + 5) + iNumber;
			acMiddleMsg[iNumber] = tCanSendData.uacData[iTempIndex];
		}
		PsCanSendDataMultiMiddleFrame(InstancePtr, acMiddleMsg, 8, iIndex + 2);
	}
	/* 发送尾帧  */
	iIndex = ((tCanSendData.usSize - 5) % 7);
	for(iNumber = 1; iNumber <= iIndex; iNumber++)
	{
		acMiddleMsg[iNumber-1] = tCanSendData.uacData[iTempIndex + iNumber];
	}
	PsCanSendDataMultiLastFrame(InstancePtr, acMiddleMsg, iIndex+1, totalFn);
}

/*************************************************************************
* 函数名称：	PsCanIdFiliter
* 功能：		ps can端口接收过滤设置
**************************************************************************
* 全局变量：		无
* 输入变量：
* 			InstancePtr					ps can设备的指针
* 输出变量：		无
* 返回值：
* 			iStatus:
* 				XST_SUCCESS		0		成功
* 				XST_FAILURE		1		失败
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.31   	 张海峰          		创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
int PsCanIdFiliter(XCanPs *InstancePtr, u32 uiFilterIndexs, u32 uiMaskValue, u32 uiIdValue)
{
	/* 状态变量  */
	int iStatus = XST_FAILURE;

	/* 关闭can id过滤器  */
	XCanPs_AcceptFilterDisable(InstancePtr, uiFilterIndexs);
	while(XCanPs_IsAcceptFilterBusy(InstancePtr) != FALSE)
	{
		/* do nothing */
	};

	/* 配置can过滤器  */
	iStatus = XCanPs_AcceptFilterSet(InstancePtr, uiFilterIndexs, uiMaskValue, uiIdValue);

	if(iStatus != XST_SUCCESS)
	{
		return XST_FAILURE;
	}

	/* 打开can id过滤器  */
	XCanPs_AcceptFilterEnable(InstancePtr, uiFilterIndexs);

	return iStatus;
}


/*************************************************************************
* 函数名称：	PsCanSendHandler
* 功能：		can发送中断处理函数
**************************************************************************
* 全局变量：		无
* 输入变量：		无
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.20   	 董瑞          		创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
static void PsCanSendHandler(void *CallBackRef)
{
	/* do nothing */
}


/*
 * ID28~ID25   |   ID24~ID20  |    ID19~ID18
 * 0001        |01100         |00
 * 0010        |01101         |01
 * 0011        |              |10
 * 0100        |              |11
 * 1000        |              |
 * 1001        |              |
 * ------------------------------------------
 *例： 预处理单机A 单帧指令传输   (0X)130
 *            单帧数据传输   (0X)1B0
 */
/*************************************************************************
* 函数名称：	PsCanRecvHandler
* 功能：		can中断接收处理函数
**************************************************************************
* 全局变量：		无
* 输入变量：
* 输出变量：
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.20   	 董瑞          		创建
* XXXX.XX.XX    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
static void PsCanRecvHandler(void *Callback)
{
    XCanPs *InstancePtr = (XCanPs *)Callback;
    int iStatus;
    u16 ID;
    u8 DLC, ADDR, SN;

  	iStatus = XCanPs_Recv(InstancePtr, RxFrame);

    //XTime_GetTime(&tCur);
    if(iStatus == XST_SUCCESS) {
        ID = (u16)((RxFrame[0] & 0xFFE00000U) >> 21u);
        DLC = (u8)((RxFrame[1] & 0xF0000000U) >> 28u);
        ADDR = (u8)((ID >> 2) & 0x1fU);
        SN = (u8)(RxFrame[2] & 0xff);

        //PsCanDataProcess(RxFrame,ID,DLC);     // 10.09
        if((can_conf.can_pre_ID != InstancePtr->CanConfig.DeviceId) && (ADDR != YW2_PREP_TELE_DATA_CAN_BC_ADDR_ID)) {
            can_conf.can_pre_ID = InstancePtr->CanConfig.DeviceId;

            if(can_conf.can_pre_ID == CAN0_DEVICE_ID) {
                can_conf.can1_rst_flag = 1;
            }
            else {
                can_conf.can0_rst_flag = 1;
            }
        }

        if(ADDR == YW2_PREP_TELE_DATA_CAN_BC_ADDR_ID) {
            switch(ID & 0x3)
            {
                /*---------单帧广播----------*/
                case 0:
                    {
                    if(can_conf.can_bc_interval < 1) {
                        can_conf.can_bc_fail = 1;
                    }
                    else {
                        can_conf.can_bc_fail = 0;
                        can_conf.can_bc_interval = 0;
                    }

                    break;
                }
                    /*---------多帧广播首帧-------*/
                case 1:
                    {
                    can_conf.can_bc_fail = 0;
                    can_conf.can_bc_lastSN = 1;
                    break;
                }
                    /*---------多帧广播中间/尾帧------*/
                case 2:
                case 3:
                    {
                    if(SN <= can_conf.can_bc_lastSN) {
                        can_conf.can_bc_fail = 1;
                    }
                    else {
                        can_conf.can_bc_lastSN = SN;
                    }
                    break;
                }

                default:
                    break;
            }
        }

        PsCanMsgType(InstancePtr, RxFrame, ID, DLC);

        can_conf.can_wait_cnt = 0;
    }
    //xil_printf("ID:%x  DLC:%d  RX1:%x  RX2:%x\r\n",ID, DLC, RxFrame[2], RxFrame[3]);
}


/*************************************************************************
* 函数名称：			PsCanMsgType
* 功能：			对ID31-28进行处理，分出不同的信息类
**************************************************************************
* 全局变量：		无
* 输入变量：		u32 *puiRxFrame,			接收的数据指针
* 				u16 usID, 					未处理的ID号
* 				u8 ucDLC					信息的dlc长度信息
* 输出变量：		无
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.10.20   	董瑞          		创建
* 2022.11.03   	张海峰         		添加了信息类型标识注释和处理方式
* 2023.01.12   	张海峰
*************************************************************************/
static void PsCanMsgType (XCanPs *InstancePtr, u32 *puiRxFrame, u16 usID, u8 ucDLC)
{
	/* 信息类型标识  */
	u8 ucMsgId = 0;
	ucMsgId = (usID >> 7) & 0xf;

	switch(ucMsgId)
	{
	 	 /* 0001  总线恢复，远程帧，无节点返回  */
	 	 case 1:
		 {
			 break;
		 }
		 /* 0010  传送指令，数据帧，节点返回数据帧 */
		 case 2:
		 {
			 /* 这里的命令为数据单帧  */
			 PsCanCommandProcess(puiRxFrame);
			 g_tEquipmentStatus.canTotalOrder ++;
			 /* 发送接收成功消息  */
			 PsCanSendSingleFrameResult(InstancePtr, YW2_PREP_ORDER_ACK_CAN_ID,XST_SUCCESS);
			 break;
		 }
		 /* 0011  用户数据注入，数据帧，节点返回数据帧  */
		 case 3:
		 {
			 /* 这里的数据一般为数据多帧，传送文件使用 */
			 PsCanDataProcess(InstancePtr, puiRxFrame, usID, ucDLC);
			 break;
		 }
		 /* 0100  遥测数据，数据帧，节点返回数据帧  */
		 case 4:
		 {
			 /* 收到消息后，返回遥测数据 */
			 g_tEquipmentStatus.canTotalOrder ++;
			 PsCanSendDataMsg(InstancePtr,puiRxFrame);
			 break;
		 }
		 /* 1000 GNC数据广播，数据帧，无节点返回  */
		 case 8:
		 {
		     GncDataProcess(InstancePtr, puiRxFrame, usID, ucDLC);
			 break;
		 }
		 /* 1001  整星对时数据，数据帧，无节点返回  */
		 case 9:
		 {
			 TimeSyncDataProcess(puiRxFrame);
			 break;
		 }
		 default:
		 {
			 break;
		 }
	 }

}

void GncDataProcess(XCanPs *InstancePtr, u32 *puiRxFrame, u16 usID, u8 ucDLC)
{
    int iIndex = 0;
    /* 当前帧序号  */
    u8 ucCurrentSN = 0;
    /* 记录上一次数据写入的数组序号  */
    u16 usLastDateIndex = 0;
    /* 记录当前数据写入的数组序号  */
    u16 usCurrentDateIndex = 0;
    /* ID19-18 帧 ，分出单帧，多帧等类  */
    switch(usID & 0x3)
    {
        /* 多帧中的首帧  */
        case 1:
        {
            /* 设置can数据的字节数  */
            tCanRecvGNCData.usSize = ucDLC;
            /* 读取帧序号  */
            ucCurrentSN = (u8)(puiRxFrame[2] & 0xff);
            if (ucCurrentSN == 1)
            {
                s_ucSN = ucCurrentSN;
            }
            s_Addit1Data.RecvGncBuffLen = 0;
            s_Addit2Data.RecvGncBuffLen = 0;
            /* 读取can数据  */
            for(iIndex = 0; iIndex < ucDLC; iIndex++)
            {
                if(iIndex < 4)
                {
                    tCanRecvGNCData.uacData[iIndex] = (u8)((puiRxFrame[2]>>(iIndex)*8)&0x000000ff);
                }
                else
                {
                     tCanRecvGNCData.uacData[iIndex] = (u8)((puiRxFrame[3]>>(iIndex)*8)&0x000000ff);
                }
                if(iIndex > 2)
                {
					s_Addit1Data.GncBuff[s_Addit1Data.RecvGncBuffLen] = tCanRecvGNCData.uacData[iIndex];
					s_Addit1Data.RecvGncBuffLen ++;
					s_Addit2Data.GncBuff[s_Addit2Data.RecvGncBuffLen] = tCanRecvGNCData.uacData[iIndex];
					s_Addit2Data.RecvGncBuffLen ++;
                }
            }
             break;
         }
        /* 多帧中的中间帧  */
        case 2:
        {
            /* 记录上一次写入数据的位置  */
            usLastDateIndex = tCanRecvGNCData.usSize;
            /* 设置can数据的字节数  */
            tCanRecvGNCData.usSize += ucDLC;
            /* 读取帧序号  */
            ucCurrentSN = (u8)(puiRxFrame[2] & 0xff);
            if (ucCurrentSN != 0)
            {
                s_ucSN = ucCurrentSN;
            }
            /* 读取can数据  */
            for(iIndex = 0; iIndex < ucDLC; iIndex++)
            {
                usCurrentDateIndex = usLastDateIndex + iIndex;
                if(iIndex < 4)
                {
                    tCanRecvGNCData.uacData[usCurrentDateIndex] = (u8)((puiRxFrame[2]>>(iIndex)*8)&0x000000ff);
                }
                else
                {
                    tCanRecvGNCData.uacData[usCurrentDateIndex] = (u8)((puiRxFrame[3]>>(iIndex)*8)&0x000000ff);
                }
				if(iIndex != 0)
				{
					s_Addit1Data.GncBuff[s_Addit1Data.RecvGncBuffLen] = tCanRecvGNCData.uacData[usCurrentDateIndex];
					s_Addit1Data.RecvGncBuffLen ++;
					s_Addit2Data.GncBuff[s_Addit2Data.RecvGncBuffLen] = tCanRecvGNCData.uacData[usCurrentDateIndex];
					s_Addit2Data.RecvGncBuffLen ++;
				}
            }
             break;
         }
        /* 多帧中的尾帧  */
         case 3:
         {
            /* 记录上一次写入数据的位置  */
            usLastDateIndex = tCanRecvGNCData.usSize;
            /* 设置can数据的字节数  */
            tCanRecvGNCData.usSize += ucDLC;
            /* 读取帧序号  */
            ucCurrentSN = (u8)(puiRxFrame[2] & 0xff);
            if (ucCurrentSN != 0)
            {
                s_ucSN = ucCurrentSN;
            }
            /* 读取can数据  */
            for(iIndex = 0; iIndex < ucDLC; iIndex++)
            {
                usCurrentDateIndex = usLastDateIndex + iIndex;
                if(iIndex < 4)
                {
                    tCanRecvGNCData.uacData[usCurrentDateIndex] = (u8)((puiRxFrame[2]>>(iIndex)*8)&0x000000ff);
                }
                else
                {
                     tCanRecvGNCData.uacData[usCurrentDateIndex] = (u8)((puiRxFrame[3]>>(iIndex)*8)&0x000000ff);
                }
				if(iIndex != 0)
				{
					s_Addit1Data.GncBuff[s_Addit1Data.RecvGncBuffLen] = tCanRecvGNCData.uacData[usCurrentDateIndex];
					s_Addit1Data.RecvGncBuffLen ++;
					s_Addit2Data.GncBuff[s_Addit2Data.RecvGncBuffLen] = tCanRecvGNCData.uacData[usCurrentDateIndex];
					s_Addit2Data.RecvGncBuffLen ++;
				}
			}
            s_Addit1Data.RecvGncRelaxLen = s_Addit1Data.RecvGncBuffLen;
            s_Addit2Data.RecvGncRelaxLen = s_Addit2Data.RecvGncBuffLen;
            for(u8 index = 0; index < s_Addit1Data.RecvGncBuffLen; index++)
            {
            	s_Addit1Data.GncRelax[index] = s_Addit1Data.GncBuff[index];
            	s_Addit2Data.GncRelax[index] = s_Addit2Data.GncBuff[index];
            }
            break;
         }
		 default:
		 {
			 break;
		 }
    }
}

/*************************************************************************
* 函数名称：	PsCanDataProcess
* 功能：		处理用户数据注入
**************************************************************************
* 全局变量：
* 输入变量：
* 输出变量：
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.11.03            张海峰          		创建
* 2022.12.26            张海峰         		添加了数据存储判断功能
*************************************************************************/
u8 sNstatusError = 0;
static void PsCanDataProcess(XCanPs *InstancePtr, u32 *puiRxFrame, u16 usID, u8 ucDLC)
{
	int iIndex = 0;

	/* 当前帧序号  */
	u8 ucCurrentSN = 0;

	/* 记录上一次数据写入的数组序号  */
	u16 usLastDateIndex = 0;

	/* 记录当前数据写入的数组序号  */
	u16 usCurrentDataIndex = 0;

	/* ID19-18 帧 ，分出单帧，多帧等类  */
	switch(usID & 0x3)
	{
		/* 单帧  */
		case 0:
		{
			/* 设置can数据的字节数  */
			tCanRecvData.usSize = ucDLC;

			/* 读取can数据  */
			for(iIndex = 0; iIndex < ucDLC; iIndex++)
			{
				if(iIndex < 4)
				{
					tCanRecvData.uacData[iIndex] = (u8)((puiRxFrame[2]>>(iIndex)*8)&0xff);
				}
				else
				{
					if(iIndex > 8)
					{
						/* DLC 大于 9 时也为 8 字节 */
						break;
					}
					else
					{
						tCanRecvData.uacData[iIndex] = (u8)((puiRxFrame[3]>>(iIndex)*8)&0xff);
					}
				}
			}

			//PsCanCommandProcess(puiRxFrame);
			/* 发送接收成功消息  */
			PsCanSendSingleFrameResult(InstancePtr, YW2_PREP_DATA_ACK_CAN_ID,XST_SUCCESS);
			break;
		}

		/* 多帧中的首帧  */
		case 1:
		{
			/* 设置can数据的字节数  */
			tCanRecvData.usSize = ucDLC;
			/* 读取帧序号  */
			ucCurrentSN = (u8)(puiRxFrame[2] & 0xff);

			/* 读取can数据  */
			for(iIndex = 0; iIndex < ucDLC; iIndex++)
			{
				if(iIndex < 4)
				{
					tCanRecvData.uacData[iIndex] = (u8)((puiRxFrame[2]>>(iIndex)*8)&0x000000ff);
				}
				else
				{
					if(iIndex > 8)
					{
						/* DLC 大于 9 时也为 8 字节 */
						break;
					}
					else
					{
						tCanRecvData.uacData[iIndex] = (u8)((puiRxFrame[3]>>(iIndex)*8)&0x000000ff);
					}
				}
			}
            /*接收到固件包首帧处理*/
            if(tCanRecvData.uacData[FIRMWARE_DEVICE_INDEX] == FIRMWARE_DEVICE_ID) {
                if(tCanRecvData.uacData[FIRMWARE_DATA_INDEX] == FIRMWARE_DATA_ID) {
                    s_FirmwareUpdate.TotalPackageNum = (tCanRecvData.uacData[FIRMWARE_TOTALPACKAGE_INDEX] << 8) + tCanRecvData.uacData[FIRMWARE_TOTALPACKAGE_INDEX + 1];
                    s_FirmwareUpdate.CurPackageSeq = (tCanRecvData.uacData[FIRMWARE_CURPACKAGE_INDEX] << 8) + tCanRecvData.uacData[FIRMWARE_CURPACKAGE_INDEX + 1];
                }
                s_FirmwareUpdate.CurPackageSN = ucCurrentSN;
                s_FirmwareUpdate.LastPackageSN = ucCurrentSN;
                s_FirmwareUpdate.RecvPackageBuffLen = 0;
				if(tCanRecvData.uacData[FIRMWARE_DATA_INDEX] > 0){
					s_FirmwareUpdate.RecvPackageType = tCanRecvData.uacData[FIRMWARE_DATA_INDEX] - 1;	
				}
            }
			 break;
		 }
		/* 多帧中的中间帧  */
		case 2:
		{
			/* 记录上一次写入数据的位置  */
			usLastDateIndex = tCanRecvData.usSize;
			/* 设置can数据的字节数  */
			tCanRecvData.usSize += ucDLC;
			/* 读取帧序号  */
			ucCurrentSN = (u8)(puiRxFrame[2] & 0xff);
			s_FirmwareUpdate.CurPackageSN = ucCurrentSN;
			/*判读帧序是否递加*/
			sNstatusError = 0;
			if((s_FirmwareUpdate.CurPackageSN-1) != s_FirmwareUpdate.LastPackageSN)
			{
				sNstatusError = 1;
			}
			else
			{
				s_FirmwareUpdate.LastPackageSN = s_FirmwareUpdate.CurPackageSN;
			}

			/* 读取can数据  */
			for(iIndex = 0; iIndex < ucDLC; iIndex++)
			{
				usCurrentDataIndex = usLastDateIndex + iIndex;
				if(iIndex < 4)
				{
					tCanRecvData.uacData[usCurrentDataIndex] = (u8)((puiRxFrame[2]>>(iIndex)*8)&0x000000ff);
				}
				else
				{
					if(iIndex > 8)
					{
						/* DLC 大于 9 时也为 8 字节 */
						break;
					}
					else
					{
						tCanRecvData.uacData[usCurrentDataIndex] = (u8)((puiRxFrame[3]>>(iIndex)*8)&0x000000ff);
					}
				}
				if(iIndex != 0)
				{
                    s_FirmwareUpdate.CompressPackBuffer[s_FirmwareUpdate.RecvPackageBuffLen] = tCanRecvData.uacData[usCurrentDataIndex];
                    s_FirmwareUpdate.RecvPackageBuffLen ++;
				}
			}

			 break;
		 }

		/* 多帧中的尾帧  */
		 case 3:
		 {
			/* 记录上一次写入数据的位置  */
			usLastDateIndex = tCanRecvData.usSize;
			/* 设置can数据的字节数  */
			tCanRecvData.usSize += ucDLC;
			/* 读取can数据  */
			for(iIndex = 0; iIndex < ucDLC; iIndex++)
			{
				usCurrentDataIndex = usLastDateIndex + iIndex;
				if(iIndex < 4)
				{
					tCanRecvData.uacData[usCurrentDataIndex] = (u8)((puiRxFrame[2]>>(iIndex)*8)&0x000000ff);
				}
				else
				{
					if(iIndex > 8)
					{
						/* DLC 大于 9 时也为 8 字节 */
						break;
					}
					else
					{
						tCanRecvData.uacData[usCurrentDataIndex] = (u8)((puiRxFrame[3]>>(iIndex)*8)&0x000000ff);
					}
				}
				if(iIndex != 0 && (iIndex < (ucDLC-2)))
				{
                    s_FirmwareUpdate.CompressPackBuffer[s_FirmwareUpdate.RecvPackageBuffLen] = tCanRecvData.uacData[usCurrentDataIndex];
                    s_FirmwareUpdate.RecvPackageBuffLen ++;

				}
				if(iIndex == (ucDLC-2))
				{
				    s_FirmwareUpdate.RecvCrc16Data = tCanRecvData.uacData[usCurrentDataIndex];
				}
                if(iIndex > (ucDLC-2))
                {
                    s_FirmwareUpdate.RecvCrc16Data = (s_FirmwareUpdate.RecvCrc16Data<<8) + tCanRecvData.uacData[usCurrentDataIndex];
                }
			}

            if(s_FirmwareUpdate.e_State != WRITE_DATA_DURING) {
                if(s_FirmwareUpdate.RecvPackageType == (DEADPIEXL_DATA_ID - 1)) {
                    if(s_FirmwareUpdate.RecvCrc16Data == CRC16(s_FirmwareUpdate.CompressPackBuffer, s_FirmwareUpdate.RecvPackageBuffLen)
                        && sNstatusError != 1 && s_FirmwareUpdate.e_State != EMMC_WR_DATA_FAIL) {
                        memcpy(s_FirmwareUpdate.CompressPackRelax, s_FirmwareUpdate.CompressPackBuffer, s_FirmwareUpdate.RecvPackageBuffLen);
                        s_FirmwareUpdate.RecvPackageRelaxLen = s_FirmwareUpdate.RecvPackageBuffLen;
                        s_FirmwareUpdate.e_State = PIXEL_MASK_BEGIN;
                         /* 发送接收成功消息  */
                         PsCanSendSingleFrameResult(InstancePtr, YW2_PREP_DATA_ACK_CAN_ID,XST_SUCCESS);
                    } else {
                        /* 发送接收失败消息  */
                        PsCanSendSingleFrameResult(InstancePtr, YW2_PREP_DATA_ACK_CAN_ID, XST_FAILURE);
                    }
                } else {
                    /*重复接收同一个包舍弃处理*/
                    if(s_FirmwareUpdate.RecvPackageStatus[s_FirmwareUpdate.CurPackageSeq - 1] != RECV_PACKAGE_CURRENT_DONE) {
                        /*crc16校验位判读正确且帧序号正确则存储一包数据*/
                        if(s_FirmwareUpdate.RecvCrc16Data == CRC16(s_FirmwareUpdate.CompressPackBuffer, s_FirmwareUpdate.RecvPackageBuffLen) && sNstatusError != 1
                            && s_FirmwareUpdate.e_State != EMMC_WR_DATA_FAIL) {
                            for(u32 index = 0; index < s_FirmwareUpdate.RecvPackageBuffLen; index++) {
                                s_FirmwareUpdate.CompressPackRelax[index] = s_FirmwareUpdate.CompressPackBuffer[index];
                            }
                            s_FirmwareUpdate.RecvPackageRelaxLen = s_FirmwareUpdate.RecvPackageBuffLen;
                            s_FirmwareUpdate.e_State = WRITE_DATA_DURING;
                            if(s_FirmwareUpdate.CurPackageSeq > s_FirmwareUpdate.EndPackageSeq) {
                                s_FirmwareUpdate.EndPackageSeq = s_FirmwareUpdate.CurPackageSeq;
                            }
                            s_FirmwareUpdate.CurPackageSeqRelax = s_FirmwareUpdate.CurPackageSeq;
                            /* 发送接收成功消息  */
                            PsCanSendSingleFrameResult(InstancePtr, YW2_PREP_DATA_ACK_CAN_ID, XST_SUCCESS);
                        } else {
                            s_FirmwareUpdate.RecvPackageStatus[s_FirmwareUpdate.CurPackageSeq - 1] = RECV_PACKAGE_CURRENT_UNDONE;
                            /* 发送接收失败消息  */
                            PsCanSendSingleFrameResult(InstancePtr, YW2_PREP_DATA_ACK_CAN_ID, XST_FAILURE);
                        }
                    } else {
                        /* 发送接收成功消息  */
                        PsCanSendSingleFrameResult(InstancePtr, YW2_PREP_DATA_ACK_CAN_ID, XST_SUCCESS);
                    }
                }
            }
			break;
		 }

		 default:
		 {
			 break;
		 }
	}
}

/*************************************************************************
* 函数名称：	PsCanCommandProcess
* 功能：		对ID31-28进行处理，分出不同的信息类
**************************************************************************
* 全局变量：
* 输入变量：
* 输出变量：
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.11.10          张海峰          		创建
* xxxx.xx.xx    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void PsCanCommandProcess(u32 *puiRxFrame)
{
	u8 ucDeviceId = 255;
	u8 ucCommand = 255;
	u8 uacData[6] = {0};

	ucDeviceId = puiRxFrame[2] & 0xff;
	ucCommand = (puiRxFrame[2] >> 8) & 0xff;
	uacData[5] = (puiRxFrame[2] >> 16) & 0xff;
	uacData[4] = (puiRxFrame[2] >> 24) & 0xff;

	uacData[0] = (puiRxFrame[3] >> 24) & 0xff;
	uacData[1] = (puiRxFrame[3] >> 16) & 0xff;
	uacData[2] = (puiRxFrame[3] >> 8) & 0xff;
	uacData[3] = puiRxFrame[3] & 0xff;

	switch(ucDeviceId)
	{
		case PRETREATMENT_ID:
		{
			PsCanPretreatmentCommandProcess(ucCommand, uacData);
			break;
		}
		case CAMERA_ONE_ID:
		{
			PsCanCameraCommandProcess(ucCommand, uacData, PL_UART_ONE);
			break;
		}
		case CAMERA_TWO_ID:
		{
			PsCanCameraCommandProcess(ucCommand, uacData, PL_UART_TWO);
			break;
		}
		case MOTOR_ID:
		{
			PsCanMotorCommandProcess(ucCommand, uacData);
			break;
		}
		case LED_ID:
		{
			PsCanLedCommandProcess(ucCommand, uacData);
			break;
		}
        case HOT_ID:
        {
            /*开启主热刀*/
            if(ucCommand == 0x01){
               SetHotKnife(1);
   			   g_tEquipmentStatus.canCompleOrder ++;
            }
            /*开启备份热刀*/
            if(ucCommand == 0x02){
               SetHotKnife(2);
   			   g_tEquipmentStatus.canCompleOrder ++;
            }
            break;
        }
        case CMA_1_PREP:
        {
            SetCMA_Prep_Para(ucCommand, uacData, 1);
            s_CamPrepFlag.cm1SaveFlag = 1;
            break;
        }
        case CMA_2_PREP:
        {
            SetCMA_Prep_Para(ucCommand, uacData, 2);
            s_CamPrepFlag.cm2SaveFlag = 1;
            break;
        }
		default:
		{
			break;
		}

	}

}


/*************************************************************************
* 函数名称：	PsCanPretreatmentCommandProcess
* 功能：		发送预处理单机整机命令
**************************************************************************
* 全局变量：
* 输入变量：
* 输出变量：
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.12.28          张海峰          		创建
* xxxx.xx.xx    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
static void PsCanPretreatmentCommandProcess(u8 ucCommand, u8 *puacData)
{
	switch(ucCommand)
	{
		case 1:
		{
			/* 切换flash版本  */
			SendChangeFlashMsg();
			g_tEquipmentStatus.canCompleOrder ++;
			break;
		}

		case 2:
		{
			/* 切换emmc版本  */
			SendChangeEmmcMsg();
			g_tEquipmentStatus.canCompleOrder ++;
			break;
		}

		case 3:
		{
			/* 升级版本 */
		    beginUpdateFirmware();
			g_tEquipmentStatus.canCompleOrder ++;
			break;
		}
        case 4:
        {
            /* 固件传输终止 */
            updateFirmwareTerm();
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 5:
        {
            /* watchdog */
        	s_SysMonit.WdtTimeOver = puacData[5];
        	s_SysMonit.WdtEnable = puacData[4];
        	s_SysRev.TestFlag = puacData[3];
        	wdtInit(s_SysMonit.WdtTimeOver,s_SysMonit.WdtEnable);
        	s_SysRev.SaveFlag = 1;
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }
        case 6:
        {
            /* 固件替换 ,关闭开门狗状态下才响应*/
        	if(s_SysMonit.WdtEnable != 1)
        	{
            	s_AdFw.cmd = puacData[5];
            	s_AdFw.key = puacData[4];
        	}
			g_tEquipmentStatus.canCompleOrder ++;
            break;
        }

		default:
		{
			break;
		}

	}
}

/*************************************************************************
* 函数名称：	PsCanCameraCommandProcess
* 功能：		处理通过can协议发送的相机命令
**************************************************************************
* 全局变量：
* 输入变量：
* 输出变量：
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.11.10          张海峰          		创建
* xxxx.xx.xx    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
static void PsCanCameraCommandProcess(u8 ucCommand, u8 *puacData, u8 ucPlUartNum)
{

	u32 uiValue = 0;

	switch(ucCommand)
	{
		case 0:
		{
		    /*关闭相机 使其进入低功耗*/
		    if(ucPlUartNum == CAMERA_ONE_ID){
		        g_sCamera1Setting.status = CAMERA_CLOSE;
				g_tEquipmentStatus.canCompleOrder ++;
		    }
		    else if(ucPlUartNum == CAMERA_TWO_ID){
		        g_sCamera2Setting.status = CAMERA_CLOSE;
				g_tEquipmentStatus.canCompleOrder ++;
		    }
			break;
		}
		case 1:
		{
		    /*打开相机 根据设置的参数来打开相机*/
            if(ucPlUartNum == CAMERA_ONE_ID){
                g_sCamera1Setting.status = CAMERA_OPEN;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
            else if(ucPlUartNum == CAMERA_TWO_ID){
                g_sCamera2Setting.status = CAMERA_OPEN;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
			break;
		}
		case 2:
		{
			/* 配置要发送的命令 */
			uiValue = puacData[5] << 24;
			uiValue |= puacData[4] << 16;
			uiValue |= puacData[3] << 8;
			uiValue |= puacData[2] ;
			if(ucPlUartNum == CAMERA_ONE_ID)
			{
	            /* 配置相机的曝光时间 */
	            /* 同步全局变量 */
	            g_sCamera1Setting.ucExposureTime[0] = puacData[2];//40
	            g_sCamera1Setting.ucExposureTime[1] = puacData[3];//42
	            g_sCamera1Setting.ucExposureTime[2] = puacData[4];//0F
	            g_sCamera1Setting.ucExposureTime[3] = puacData[5];//00
	            g_tEquipmentStatus.uiCamera_1_ExposureTime = uiValue;
	            g_sCamera1Setting.commandTrig = A3_COMMAND;
				g_tEquipmentStatus.canCompleOrder ++;
	            s_Addit1Data.AdditNum = ((g_tEquipmentStatus.uiCamera_1_ExposureTime+g_tEquipmentStatus.ucCamera_1_ShootTime)/1000000);
	            SetFpga3CEachPic(ucPlUartNum, s_Addit1Data.AdditNum);
			}
			else if(ucPlUartNum == CAMERA_TWO_ID)
			{
	            /* 配置相机的曝光时间 */
	            /* 同步全局变量 */
	            g_sCamera2Setting.ucExposureTime[0] = puacData[2];
	            g_sCamera2Setting.ucExposureTime[1] = puacData[3];
	            g_sCamera2Setting.ucExposureTime[2] = puacData[4];
	            g_sCamera2Setting.ucExposureTime[3] = puacData[5];
			    g_tEquipmentStatus.uiCamera_2_ExposureTime = uiValue;
			    g_sCamera2Setting.commandTrig = A3_COMMAND;
				g_tEquipmentStatus.canCompleOrder ++;
	            s_Addit2Data.AdditNum = ((g_tEquipmentStatus.uiCamera_2_ExposureTime+g_tEquipmentStatus.ucCamera_2_ShootTime)/1000000);
	            SetFpga3CEachPic(ucPlUartNum, s_Addit2Data.AdditNum);
			}

	        SetFpgaExposureTime(ucPlUartNum, uiValue);//向PL端发送相机曝光时间
			break;
		}
		case 3:
		{
            if(ucPlUartNum == CAMERA_ONE_ID)
            {
                /* 配置相机增益 */
                g_sCamera1Setting.ucGain[0] = puacData[4];
                g_sCamera1Setting.ucGain[1] = puacData[5];
                g_sCamera1Setting.ucAnaGain[0] = puacData[2];
                g_sCamera1Setting.ucAnaGain[1] = puacData[3];

    			uiValue = puacData[5] << 8;
    			uiValue |= puacData[4] ;
                g_tEquipmentStatus.ucCamera_1_GainRGB = uiValue;

    			uiValue = puacData[3] << 8;
    			uiValue |= puacData[2] ;
                g_tEquipmentStatus.ucCamera_1_GainAnalog = uiValue;

                g_sCamera1Setting.commandTrig = A4_COMMAND;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
            else if(ucPlUartNum == CAMERA_TWO_ID)
            {
                /* 配置相机增益 */
                g_sCamera2Setting.ucGain[0] = puacData[4];
                g_sCamera2Setting.ucGain[1] = puacData[5];
                g_sCamera2Setting.ucAnaGain[0] = puacData[2];
                g_sCamera2Setting.ucAnaGain[1] = puacData[3];

    			uiValue = puacData[5] << 8;
    			uiValue |= puacData[4] ;
                g_tEquipmentStatus.ucCamera_2_GainRGB = uiValue;

    			uiValue = puacData[3] << 8;
    			uiValue |= puacData[2] ;
                g_tEquipmentStatus.ucCamera_2_GainAnalog = uiValue;

                g_sCamera2Setting.commandTrig = A4_COMMAND;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
			SetFpgaGain(ucPlUartNum,uiValue);
			break;
		}
		case 4:
		{
            if(ucPlUartNum == CAMERA_ONE_ID)
            {
                g_sCamera1Setting.ucGainExtend[0] = puacData[4];
                g_sCamera1Setting.ucGainExtend[1] = puacData[5];
                g_sCamera1Setting.ucGainHLGC[0] = puacData[2];
                g_sCamera1Setting.ucGainHLGC[1] = puacData[3];
                g_tEquipmentStatus.usCamera_1_GainEx = puacData[5] << 8;
                g_tEquipmentStatus.usCamera_1_GainEx |= puacData[4] ;
                g_tEquipmentStatus.usCamera_1_HGCLGC = puacData[3] << 8;
                g_tEquipmentStatus.usCamera_1_HGCLGC |= puacData[2] ;
                g_sCamera1Setting.commandTrig = A4_COMMAND;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
            else if(ucPlUartNum == CAMERA_TWO_ID)
            {
                g_sCamera2Setting.ucGainExtend[0] = puacData[4];
                g_sCamera2Setting.ucGainExtend[1] = puacData[5];
                g_sCamera2Setting.ucGainHLGC[0] = puacData[2];
                g_sCamera2Setting.ucGainHLGC[1] = puacData[3];
                g_tEquipmentStatus.usCamera_2_GainEx = puacData[5] << 8;
                g_tEquipmentStatus.usCamera_2_GainEx |= puacData[4];
                g_tEquipmentStatus.usCamera_2_HGCLGC = puacData[3] << 8;
                g_tEquipmentStatus.usCamera_2_HGCLGC |= puacData[2] ;
                g_sCamera2Setting.commandTrig = A4_COMMAND;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
			break;
		}
		case 5:
		{
            if(ucPlUartNum == CAMERA_ONE_ID)
            {
                g_sCamera1Setting.ucOffsetR[0] = puacData[4];
                g_sCamera1Setting.ucOffsetR[1] = puacData[5];
                g_tEquipmentStatus.ucCamera_1_Offset = puacData[5] << 8;
                g_tEquipmentStatus.ucCamera_1_Offset |= puacData[4] ;
                g_sCamera1Setting.commandTrig = A8_COMMAND;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
            else if(ucPlUartNum == CAMERA_TWO_ID)
            {
                g_sCamera2Setting.ucOffsetR[0] = puacData[4];
                g_sCamera2Setting.ucOffsetR[1] = puacData[5];
                g_tEquipmentStatus.ucCamera_2_Offset = puacData[5] << 8;
                g_tEquipmentStatus.ucCamera_2_Offset |= puacData[4] ;
                g_sCamera2Setting.commandTrig = A8_COMMAND;
    			g_tEquipmentStatus.canCompleOrder ++;
           }
			break;
		}
		case 6:
		{
            if(ucPlUartNum == CAMERA_ONE_ID)
            {
                if(puacData[5] == TEST_IMAGE){
                    g_sCamera1Setting.ucClPicSet = 2;
                    g_sCamera2Setting.ucClPicSet = 2;
                    uiValue = 0x02;
                }
                else {
                    g_sCamera1Setting.ucClPicSet = 0;
                    g_sCamera2Setting.ucClPicSet = 0;
                }
              g_tEquipmentStatus.ucCamera_1_WorkMode = puacData[5];
              g_tEquipmentStatus.ucCamera_2_WorkMode = puacData[5];
              g_sCamera2Setting.commandTrig = B3_COMMAND;
              g_sCamera1Setting.commandTrig = B3_COMMAND;
  			g_tEquipmentStatus.canCompleOrder ++;
            }
            if(puacData[5] == 0){
            	uiValue = 0x01;
            }
            else if(puacData[5] == 1){
            	uiValue = 0x02;
            }
            else if(puacData[5] == 2){
            	uiValue = 0x04;
            }
            else if(puacData[5] == 3){
            	uiValue = 0x08;
            	s_Cam1CaliData.FlatRload = 1;
            }
            SetFpgaWorkMode(ucPlUartNum, uiValue);
			break;
		}
		case 7:
		{
            uiValue = puacData[5] << 8;
            uiValue |= puacData[4];
            if(ucPlUartNum == CAMERA_ONE_ID)
            {
                g_tEquipmentStatus.ucCamera_1_ShootNum = uiValue;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
            else if(ucPlUartNum == CAMERA_TWO_ID)
            {
                g_tEquipmentStatus.ucCamera_2_ShootNum = uiValue;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
            SetFpgaPicNum(ucPlUartNum, uiValue);
			break;
		}
		case 8:
		{
            uiValue = puacData[2];
            uiValue |= (puacData[3] << 8);
            uiValue |= (puacData[4] << 16);
            uiValue |= (puacData[5] << 24);
            if(ucPlUartNum == CAMERA_ONE_ID)
            {
                g_tEquipmentStatus.ucCamera_1_ShootTime = uiValue;
    			g_tEquipmentStatus.canCompleOrder ++;
	            s_Addit1Data.AdditNum = ((g_tEquipmentStatus.uiCamera_1_ExposureTime+g_tEquipmentStatus.ucCamera_1_ShootTime)/1000000);
	            SetFpga3CEachPic(ucPlUartNum, s_Addit1Data.AdditNum);
            }
            else if(ucPlUartNum == CAMERA_TWO_ID)
            {
                g_tEquipmentStatus.ucCamera_2_ShootTime = uiValue;
    			g_tEquipmentStatus.canCompleOrder ++;
	            s_Addit2Data.AdditNum = ((g_tEquipmentStatus.uiCamera_2_ExposureTime+g_tEquipmentStatus.ucCamera_2_ShootTime)/1000000);
	            SetFpga3CEachPic(ucPlUartNum, s_Addit2Data.AdditNum);
            }
            SetFpgaShotTimeLapse(ucPlUartNum, uiValue);
			break;
		}
		case 9:
		{
            if(ucPlUartNum == CAMERA_ONE_ID && g_tEquipmentStatus.ucCamera_1_status == CAMERA_OPEN && Camera1ShootState == WAIT_SHOOT)
            {
                Camera1ShootState = BEGIN_SHOOT;
    			g_tEquipmentStatus.canCompleOrder ++;
            }
            else if(ucPlUartNum == CAMERA_TWO_ID && g_tEquipmentStatus.ucCamera_2_status == CAMERA_OPEN && Camera2ShootState == WAIT_SHOOT)
            {
                Camera2ShootState = BEGIN_SHOOT;
    			g_tEquipmentStatus.canCompleOrder ++;
            }

			break;
		}
		
        case 10:
        {
            /*重置相机Mask 恢复为坏点出厂Mask*/
            if(ucPlUartNum == CAMERA_ONE_ID) {
                g_sCamera1Setting.PixelMaskRstFlag = 1;
                g_tEquipmentStatus.canCompleOrder++;
            } else if(ucPlUartNum == CAMERA_TWO_ID) {
                g_sCamera2Setting.PixelMaskRstFlag = 1;
                g_tEquipmentStatus.canCompleOrder++;
            }
            break;
        }		
		
		default:
		{
			break;
		}
	}

}

/*************************************************************************
* 函数名称：	PsCanMotorCommandProcess
* 功能：		发送电机命令
**************************************************************************
* 全局变量：
* 输入变量：
* 输出变量：
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.12.28          张海峰          		创建
* xxxx.xx.xx    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
static void PsCanMotorCommandProcess(u8 ucCommand, u8 *puacData)
{
	u32 uiDirection = 0;
	u32 uiSteps = 0;

	switch(ucCommand)
	{
	    /*30000步数大约100度*/
	    /*电机1控制*/
		case 1:
		{
            uiDirection = puacData[5];
            uiSteps = (puacData[4] << 8);
            uiSteps |= puacData[3];
            g_tEquipmentStatus.ucMotor_1_Angle = (uiSteps/200);
            MotorControl(ucCommand, uiDirection, uiSteps);
			g_tEquipmentStatus.canCompleOrder ++;
           break;
		}
		/*电机2控制*/
		case 2:
		{
            uiDirection = puacData[5];
            uiSteps = (puacData[4] << 8);
            uiSteps |= puacData[3];
            g_tEquipmentStatus.ucMotor_2_Angle = (uiSteps/200);
            MotorControl(ucCommand, uiDirection, uiSteps);
			g_tEquipmentStatus.canCompleOrder ++;
            break;
		}
		default:
		{
			break;
		}

	}

}

/*************************************************************************
* 函数名称：	PsCanLedCommandProcess
* 功能：		发送LED命令
**************************************************************************
* 全局变量：
* 输入变量：
* 输出变量：
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.12.28          张海峰          		创建
* xxxx.xx.xx    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
static void PsCanLedCommandProcess(u8 ucCommand, u8 *puacData)
{
	u32 uiPwm = 0;
	u32 uiFreq = 0;

	switch(ucCommand)
	{
	    /*LED*/
		case 1:
		{
			uiPwm = (puacData[5] << 8);
			uiPwm |= (puacData[4] << 0);
			g_tEquipmentStatus.ledBrightness = uiPwm/10;
			uiFreq =  200000/puacData[3];
			uiPwm = uiPwm*(uiFreq/1000);
			LedControl(1,1, uiPwm, uiFreq);
			g_tEquipmentStatus.canCompleOrder ++;
			break;
		}
		default:
		{
			break;
		}
	}
}


/*************************************************************************
* 函数名称：	TimeSyncDataProcess
* 功能：		整星对时数据处理
**************************************************************************
* 全局变量：
* 输入变量：
* 输出变量：
* 返回值：                       	无
*-----------------------------------------------------------------------
* 日期          		修改人        		修改内容
*-----------------------------------------------------------------------
* 2022.12.28          张海峰          		创建
* xxxx.xx.xx    XXX         添加/修改了XXXXXXXXXXX
*************************************************************************/
void TimeSyncDataProcess(u32 *puiRxFrame)
{
	u8 uacData[6] = {0};
	u32 uiSecond = 0;

	/* W1,W2为星上时间秒内毫秒数，范围0~999，低字节在前，
	 * 高字节在后。W3~W6为星上时间自UTC时间2000年1月1日12时起秒累加值，低字节在前，高字节在后。 */

	uacData[5] = (puiRxFrame[2] >> 16) & 0xff;//W3
	uacData[4] = (puiRxFrame[2] >> 24) & 0xff;//W4
	uacData[2] = (puiRxFrame[3] >> 8) & 0xff;//W6
	uacData[3] = puiRxFrame[3] & 0xff;//W5

	uiSecond = uacData[5];			//W3
	uiSecond |= (uacData[4] << 8);  //W4
	uiSecond |= (uacData[3] << 16); //W5
	uiSecond |= (uacData[2] << 24); //W6
	//g_tEquipmentStatus.uiSecond = uiSecond;

    Xil_Out32(TIME_ENABLE, 1);
    Xil_Out32(TIME_ENABLE, 0);

	/* 配置fpga的时间寄存器  */
	SetFpgaTime(uiSecond);
}


